#! /bin/bash
### BEGIN INIT INFO
# Provides:          n2n-edge
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: n2n-edge
# Description:       Start n2n edge node daemon
### END INIT INFO

# Init script for n2n edge node
# Copyright (C) 2010  Kim Hansen <kim@i9.dk>
# Refactor (C) 2017 Dale MM0HGW <mm0hgw@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="n2n edge"
NAME=n2n-edge
DAEMON=/usr/sbin/edge
SCRIPTNAME=/etc/init.d/$NAME

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Check config
if [ -z "$N2N_EDGE_CONFIG_DONE" ]
then
	echo "Warning: n2n-edge not configured, edit config file in /etc/default/$NAME." 1>&2
        exit 0
fi

# Load default values
if [ -z $HOSTNAME ]; then 
	HOSTNAME=`uname -n`
fi
if [ -z ${N2N_SN_HOST+x} ]; then 
	N2N_SN_HOST=localhost; 
fi
if [ -z ${N2N_SN_PORT+x} ]; then 
	N2N_SN_PORT=7654; 
fi
DAEMON_ARGS="$DAEMON_ARGS  -l $N2N_SN_HOST:$N2N_SN_PORT"
if [ -z ${N2N_IP+x} ]; then 
	N2N_IP_ADDR=`getent hosts $HOSTNAME | awk '{print  $1}'`
	N2N_IP="static:$N2N_IP_ADDR"
fi
if [ -z $N2N_IP ]; then
	N2N_IP="dhcp:0.0.0.0"
fi	
DAEMON_ARGS="$DAEMON_ARGS -a $N2N_IP";
if [ -z ${N2N_MAC+x} ]; then
	N2N_MAC=`getent ethers $HOSTNAME | awk '{print  $1}'`; 
fi
if ! [ -z $N2N_MAC ]; then 
	DAEMON_ARGS="$DAEMON_ARGS -m $N2N_MAC"; 
fi
DAEMON_ARGS="$DAEMON_ARGS -c $N2N_COMMUNITY"

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
	# Return
	#   0 if daemon has been started
	#   1 if daemon was already running
	#   2 if daemon could not be started
	start-stop-daemon --start --quiet --user nobody --exec $DAEMON --test \
		|| return 1
	export N2N_KEY
	start-stop-daemon --start --quiet --user nobody --exec $DAEMON -- \
	 -u $(id -u nobody) -g $(id -g nobody) $DAEMON_ARGS \
		|| return 2
}

#
# Function that stops the daemon/service
#
do_stop()
{
	# Return
	#   0 if daemon has been stopped
	#   1 if daemon was already stopped
	#   2 if daemon could not be stopped
	#   other if a failure occurred
	start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --user nobody --exec $DAEMON
}

case "$1" in
  start)
	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
	do_start
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  stop)
	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
	do_stop
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  restart|force-reload)
	log_daemon_msg "Restarting $DESC" "$NAME"
	do_stop
	case "$?" in
	  0|1)
		do_start
		case "$?" in
			0) log_end_msg 0 ;;
			1) log_end_msg 1 ;; # Old process is still running
			*) log_end_msg 1 ;; # Failed to start
		esac
		;;
	  *)
	  	# Failed to stop
		log_end_msg 1
		;;
	esac
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
	exit 3
	;;
esac

true  # Set exit status to 0 (succes)
